package com.woniu.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.order.client.CommentManageClient;
import com.woniu.order.client.param.AdditionalCommentParam;
import com.woniu.order.client.param.AddCommentParam;
import com.woniu.order.common.enums.CommentStatusEnum;
import com.woniu.order.infra.model.Comment;
import com.woniu.order.service.CommentAttachmentService;
import com.woniu.order.service.CommentService;
import com.woniu.order.service.param.AddCommentServiceParam;
import com.woniu.order.service.param.AdditionalCommentServiceParam;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@Slf4j
public class CommentManageApiImpl implements CommentManageClient {

    @Resource
    private CommentService commentService;

    @Resource
    private CommentAttachmentService commentAttachmentService;

    /**
     * 评论
     *
     * @param addCommentParam
     * @return
     */
    @Override
    public Result addComment(AddCommentParam addCommentParam) {
        log.info("评论：{}", addCommentParam);
        AddCommentServiceParam addCommentServiceParam = BeanUtil.copyProperties(addCommentParam, AddCommentServiceParam.class);
        commentService.addComment(addCommentServiceParam);
        return Result.getInstance();
    }

    /**
     * 追加评论
     *
     * @param additionalCommentParam
     * @return
     */
    @Override
    public Result addAdditionalComment(AdditionalCommentParam additionalCommentParam) {
        log.info("追加评论：{}", additionalCommentParam);
        AdditionalCommentServiceParam additionalCommentServiceParam = BeanUtil.copyProperties(additionalCommentParam, AdditionalCommentServiceParam.class);
        commentService.addAdditionalComment(additionalCommentServiceParam);
        return Result.getInstance();
    }

    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @Override
    public Result deleteComment(Integer commentId) {
        log.info("删除评论：{}", commentId);
        Comment comment = commentService.getById(commentId);
        comment.setState(CommentStatusEnum.DELETE.getCode());
        commentService.updateById(comment);
        return Result.getInstance();
    }

    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public Result uploadFile(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file);
        String path = commentAttachmentService.saveFile(file);
        return Result.getInstance(String.class).setData(path);
    }
}
